<?php
    class Check_in extends CI_Model{

        public function __construct(){

            parent::__construct();
            $this->load->database();
        }
        // public function erp_connect($RY_name)
        // {
        //     if ($RY_name == '') {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         //调用U8接口获取人员数据
        //         $serverName = "192.168.10.20"; //服务器地址
        //         $username = "dorm"; //数据库用户名
        //         $password = "123456"; //数据库密码
        //         $connectionInfo = array("UID" => $username, "PWD" => $password, "Database" => "UFDATA_666_2017");
        //         $conn = sqlsrv_connect($serverName, $connectionInfo);
        //         if ($conn) {
        //             $sj1 = " cPsn_Name = '" . $RY_name . "'";
        //             // $sql = 'SELECT * FROM hr_hi_person a inner join Department b ON a.cDept_num = b.cDepCode WHERE' ."$sj1";
        //             $sql = 'SELECT A.JobNumber ,A.vIDNo ,A.rSex ,A.rNativePlace ,A.rPerResidence FROM hr_hi_person AS A WHERE' . $sj1;
        //             $query = iconv("UTF-8", "GB2312//IGNORE", $sql);
        //             $params = array();
        //             $options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
        //             $return = sqlsrv_query($conn, $query, $params, $options);  //$conn资源句柄
        //             if ($return == true) {
        //                 if ($row = sqlsrv_fetch_array($return, SQLSRV_FETCH_ASSOC)) {
        //                     $num = iconv("GB2312", "UTF-8//IGNORE", $row['JobNumber']);
        //                     $vid = iconv("GB2312", "UTF-8//IGNORE", $row['vIDNo']);
        //                     $Sex = iconv("GB2312", "UTF-8//IGNORE", $row['rSex']);
        //                     // $rNa = iconv("GB2312", "UTF-8//IGNORE",$row['rNativePlace']);
        //                     $rPe = iconv("GB2312", "UTF-8//IGNORE", $row['rPerResidence']);
        //                     $rNa = substr($vid, 0, 6);
        //                     $this->db->select('hometown_description');
        //                     $this->db->where('hometown_number', $rNa);
        //                     $home_town = $this->db->get('dorm_hometown');
        //                     if ($home_town->num_rows() == 0) {
        //                         $return_fa = array('res' => '40000', 'msg' => '此人所属籍贯区域编号已撤销或在系统中不存在请手动录入', 'data' => array('SQ_RY_number' => $num, 'SQ_RY_idcardnum' => $vid, 'SQ_RY_sex' => $Sex));
        //                         $result = json_encode($return_fa);
        //                     } else {
        //                         $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => array('SQ_RY_number' => $num, 'SQ_RY_idcardnum' => $vid, 'SQ_RY_sex' => $Sex, 'SQ_RY_address' => $rPe, 'SQ_RY_location' => $home_town->row()->hometown_description));
        //                         $result = json_encode($return_su);
        //                     }
        //                 } else {
        //                     $return_fa = array('res' => '40000', 'msg' => 'ERP无此人员记录', 'data' => '');
        //                     $result = json_encode($return_fa);
        //                 }
        //             } else {
        //                 $return_fa = array('res' => '40001', 'msg' => '请检查sqlsrv_query参数', 'data' => $return);
        //                 $result = json_encode($return_fa);
        //             }
        //             return $result;
        //         } else {
        //             echo "连接失败！";
        //             $error = eval('return ' . iconv("gbk", "utf-8", var_export(sqlsrv_errors(), true) . ';'));
        //             var_dump($error);
        //             exit;
        //         }
        //     }
        // }
        
        //入住申请人员查询
        public function check_in_people_select($name, $idNumber, $limit){
            if($limit != ''){
                $limit1 = ("$limit" - 1) * 10;
            }
            $sj1 = $name == '' ? ' 1 = 1 ':" name LIKE '%".$name."%'";
            $sj2 = $idNumber  == '' ? ' AND 1 = 1 ':" AND idNumber LIKE '%".$idNumber."%'";
            $sj3 = $limit == '' ? ' AND 1 = 1 ' : " Limit " . $limit1 . ",10";
            if($limit != ''){
                $sql = "SELECT address ,sex ,department ,job ,phone ,name ,idNumber ,room_number FROM dorm_stay_retreat_tune WHERE".$sj1 .$sj2 .$sj3;
                $query = $this->db->query($sql ,$sj1 ,$sj2 ,$sj3);
                $return = $this->db->affected_rows();
            }else{
                $sql = "SELECT address ,sex ,department ,job ,phone ,name ,idNumber ,room_number FROM dorm_stay_retreat_tune WHERE".$sj1 .$sj2;
                $query = $this->db->query($sql ,$sj1 ,$sj2);
                $return = $this->db->affected_rows();
            }
            if($return){
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                $result = json_encode($return_su);
            } else {
                $return_fa = array('res' => '40001', 'msg' =>'查询失败', 'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        // 入住方法：申请人节点
        public function check_in_applicant($name, $sex, $idNumber, $address, $phone, $emergencyContact, $relationShip, $emergencyPhone, $department, $job, $jobNumber, $temporary){
            //临时入住
            if($temporary == 1){
                if($name == '' || $idNumber == '' || $sex == '' || $address == ''){
                    $return = array('res' => '40002', 'msg' => '请输入必填字段', 'data' => '');
                    $result = json_encode($return);
                } else {
                    $date_now = date('Y-m-d');
                    $data_stay_retreat_tune = array(
                        'name' => $name,                            //姓名
                        'sex' => $sex,                              //性别
                        'idNumber' => $idNumber,                    //身份证号
                        'address' => $address,                      //户籍地址
                        'emergencyContact' => $emergencyContact,    //紧急联系人
                        'phone' => $phone,                          //联系方式
                        'applyName' => $name,                       //姓名
                        'apply_date' => $date_now,                  //申请日期
                        'type' => 1                                 //类别
                    );
                    $this->db->insert('dorm_stay_retreat_tune' ,$data_stay_retreat_tune);
                    $return = $this->db->affected_rows();
                    if($return){
                        //申请成功
                        $return_su = array('res' => '40000', 'msg' => '申请成功', 'data' => $name);
                        $result = json_encode($return_su);
                    } else {
                        //申请失败
                        $return_fa = array('res' => '40001', 'msg' =>'申请失败', 'data' => '');
                        $result = json_encode($return_fa);
                    }
                }
            } else {
                if($name == '' || $sex == '' || $idNumber == '' || $address == '' || $phone == '' || $emergencyContact == '' || $relationShip == '' || $emergencyPhone == '' || $department == '' || $job == ''){
                    $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                    $result = json_encode($return);
                } else {
                    $query = $this->db->get_where('dorm_stay_retreat_tune' ,array('idNumber' => $idNumber, 'area_number' => '', 'floor_number' => '', 'dorm_floor' => '', 'room_number' => ''));
                    if($query->num_rows()>0){
                        $return = array('res' => '40002', 'msg' => '已申请，不允许重复申请' ,'data' => '');
                        $result = json_encode($return);
                    } else {
                        //将数据插入入住/退宿/调宿表
                        $date_now = date('Y-m-d');
                        $data_stay_retreat_tune = array(
                            'name' => $name,
                            'sex' => $sex,
                            'idNumber' => $idNumber,
                            'address' => $address,
                            'address' => $address,
                            'phone' => $phone,
                            'emergencyContact' => $emergencyContact,
                            'relationShip' => $relationShip,
                            'emergencyPhone' => $emergencyPhone,
                            'department' => $department,
                            'job' => $job,
                            'jobNumber' => $jobNumber,
                            'applyName' => $name,
                            'apply_date' => $date_now,
                            'type' => 1
                        );
                        $this->db->insert('dorm_stay_retreat_tune' ,$data_stay_retreat_tune);
                        $return = $this->db->affected_rows();

                        if($return){
                            //申请成功
                            $return_su = array('res' => '40000', 'msg' => '申请成功', 'data' => $name);
                            $result = json_encode($return_su);
                        } else {
                            //申请失败
                            $return_fa = array('res' => '40001', 'msg' =>'申请失败', 'data' => '');
                            $result = json_encode($return_fa);
                        }
                    }
                }
            }
            return $result;
        }

        
        // 入住方法:宿管员节点
        public function check_in_admin($name ,$idNumber ,$liveDate ,$area_number, $floor_number, $dorm_floor, $room_number, $bedNumber ,$remarks){
            //人员姓名/身份证号/入住日期/区域编号/宿舍楼编号/宿舍楼层/宿舍房间号/床位号
            if ( $name == '' || $idNumber == '' || $liveDate == '' || $area_number == '' || $floor_number == '' || $dorm_floor == '' || $room_number == '' || $bedNumber == '' ) {
                //必输字段未输则报错
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                //判断人员信息表中是否有此人员的信息，若已有则报错
                // $this->db->select('*');
                // $this->db->where('RY_idcardnum' ,$RY_idcardnum);
                // $query_people = $this->db->get('dorm_livepeople');
                $query_people = $this->db->get_where('dorm_livepeople' ,array('idNumber' => $idNumber));
                if($query_people->num_rows()>0){
                    $return = array('res' => '40002', 'msg' => '已入住，不允许重复入住' ,'data' => '');
                    $result = json_encode($return);
                } else {
                    //取人员信息数据
                    // $this->db->select('*');
                    // $this->db->where('SQ_RY_idcardnum' ,$RY_idcardnum);
                    // $query = $this->db->get('dorm_stay_retreat_tune');
                    $query = $this->db->get_where('dorm_stay_retreat_tune' ,array('idNumber' => $idNumber));
                    //将数据更新到入住/退宿/调宿表
                    $data_stay_retreat_tune = array(
                        'area_number' => $area_number,      //区域编号
                        'floor_number' => $floor_number,    //宿舍楼编号
                        'dorm_floor' => $dorm_floor,        //宿舍楼层号
                        'room_number' => $room_number,      //宿舍房间号
                        'bedNumber' => $bedNumber,          //床位号
                        'liveDate' => $liveDate             //入住日期
                    );
                    $this->db->where('idNumber' ,$idNumber); 
                    $this->db->update('dorm_stay_retreat_tune' ,$data_stay_retreat_tune);
                    $query_stay_retreat_tune = $this->db->affected_rows();
                
                    $check_in_address = $area_number .'*' .$floor_number .'*' .$room_number .'*' .$bedNumber;
                    //若人员工号不为空将工号更新到表中若为空则对工号字段不进行更新
                    if($query->row()->jobNumber != '') {
                        //将数据插入人员入住信息
                        $data_livepeople = array(
                            'area_number' => $area_number,                              //区域编号
                            'floor_number' => $floor_number,                            //宿舍楼编号
                            'dorm_floor' => $dorm_floor,                                //宿舍楼层号
                            'room_number' => $room_number,                              //宿舍房间号
                            'check _inAddress' => $check_in_address,                    //入住位置
                            'name' => $name,                                            //人员姓名
                            'jobNumber' => $query->row()->jobNumber,                    //工号
                            'degsonid' => $query->row()->degsonid,                      //人员编码
                            'idNumber' => $idNumber,                                    //身份证号
                            'sex' => $query->row()->sex,                                //性别
                            'department' => $query->row()->department,                  //部门
                            'job' => $query->row()->job,                                //岗位
                            'location' => $query->row()->location,                      //籍贯
                            'phone' => $query->row()->phone,                            //联系电话
                            'emergencyContact' => $query->row()->emergencyContact,      //紧急联系人
                            'relationShip' => $query->row()->relationShip,              //与紧急联系人关系
                            'emergencyPhone' => $query->row()->emergencyPhone,          //紧急联系人电话
                            'liveCheck' => 'X',                                         //是否已入住
                            'liveDate' => $liveDate,                                    //入住日期
                            'bedNumber' => $bedNumber,                                  //床位号
                            'remarks' => $remarks,                                      //备注
                        );
                        
                        //信息写入床位
                        $data_bed = array(
                            'area_number' => $area_number,                              //区域编号
                            'floor_number' => $floor_number,                            //宿舍楼编号
                            'dorm_floor' => $dorm_floor,                                //宿舍楼层号
                            'room_number' => $room_number,                              //宿舍房间号
                            'idNumber' => $idNumber,                                    //身份证号
                            'name' => $name,                                            //人员姓名
                            'jobNumber' => $query->row()->jobNumber,                    //工号
                            'sex' => $query->row()->sex,                                //性别
                            'department' => $query->row()->department,                  //部门
                            'phone' => $query->row()->phone,                            //联系电话
                            'bedStatus' => 1,                                           //床位状态 1:开启 0:关闭
                            'check_inWhether' => 1,                                     //床位是否住人
                            'bedNumber' => $bedNumber,                                  //床位号
                            'remarks' => $remarks                                       //备注
                        );

                        //信息写入人天水电
                        $data_manday = array(
                            'area_number' => $area_number,                              //区域编号
                            'floor_number' => $floor_number,                            //宿舍楼编号
                            'room_number' => $room_number,                              //宿舍房间号
                            'name' => $name,                                            //人员姓名
                            'jobNumber' => $query->row()->jobNumber,                    //工号
                            'dorm_livedate' => $liveDate,                              //宿舍入住日期
                            'room_livedate' => $liveDate                               //房间入住日期
                        );

                    } else {
                        //将数据插入人员入住信息
                        $data_livepeople = array(
                            'area_number' => $area_number,                              //区域编号
                            'floor_number' => $floor_number,                            //宿舍楼编号
                            'dorm_floor' => $dorm_floor,                                //宿舍楼层号
                            'room_number' => $room_number,                              //宿舍房间号
                            'check_inAddress' => $check_in_address,                     //入住位置
                            'name' => $name,                                            //人员姓名
                            'idNumber' => $idNumber,                                    //身份证号
                            'sex' => $query->row()->sex,                                //性别
                            'department' => $query->row()->department,                  //部门
                            'job' => $query->row()->job,                                //岗位
                            'location' => $query->row()->location,                      //籍贯
                            'phone' => $query->row()->phone,                            //联系电话
                            'emergencyContact' => $query->row()->emergencyContact,      //紧急联系人
                            'relationShip' => $query->row()->relationShip,              //与紧急联系人关系
                            'emergencyPhone' => $query->row()->emergencyPhone,          //紧急联系人电话
                            'liveCheck' => 'X',                                         //是否入住
                            'liveDate' => $liveDate,                                    //入住日期
                            'bedNumber' => $bedNumber,                                  //床位号
                            'remarks' => $remarks,                                      //备注
                        );
                        //信息写入床位
                        $data_bed = array(
                            // 'area_number' => $area_number,                           //区域编号
                            // 'floor_number' => $floor_number,                         //宿舍楼编号
                            // 'dorm_floor' => $dorm_floor,                             //宿舍楼层号
                            // 'room_number' => $room_number,                           //宿舍房间号
                            'idNumber' => $idNumber,                                    //身份证号
                            'name' => $name,                                            //人员姓名
                            'sex' => $query->row()->sex,                                //性别
                            'department' => $query->row()->department,                  //部门
                            'phone' => $query->row()->phone,                            //联系电话
                            'bedStatus' => 1,                                           //床位状态
                            'check_inWhether' => 1,                                     //床位是否住人
                            'bedNumber' => $bedNumber,                                  //床位号
                            'remarks' => $remarks                                       //备注
                        );

                        //信息写入人天水电
                        $data_manday = array(
                            'area_number' => $area_number,//区域编号
                            'floor_number' => $floor_number,//宿舍楼编号
                            'room_number' => $room_number,//宿舍房间号
                            'name' => $name,//人员姓名
                            'jobNumber' => $query->row()->jobNumber,//工号
                            'dorm_livedate' => $liveDate,//宿舍入住日期
                            'room_livedate' => $liveDate//房间入住日期
                        );
                    }
                    //将人员信息插入到人员信息表中
                    $this->db->insert('dorm_livepeople' ,$data_livepeople);
                    $query_livepeople = $this->db->affected_rows();

                    //将人员信息更新到床位表中
                    $where_bed = array('area_number' => $area_number ,'floor_number' => $floor_number ,'room_number' => $room_number ,'bedNumber' => $bedNumber);
                    $this->db->where($where_bed);
                    $this->db->update('dorm_bed' ,$data_bed);
                    $query_bed = $this->db->affected_rows();
                    
                    //取入住记录信息进行展示
                    // $where = array('idNumber' => $idNumber);
                    $where3 = array('area_number' => $area_number ,'floor_number' => $floor_number ,'room_number' => $room_number);
                    // $this->db->select('*');
                    // $this->db->where($where);
                    // $query1 = $this->db->get('dorm_stay_retreat_tune');
                    $query1 = $this->db->get_where('dorm_stay_retreat_tune' ,array('idNumber' => $idNumber));

                    //取这个房间的实际入住人数进行更新
                    $select = array('peopleNumber' ,'remainNumber');
                    $this->db->select($select);
                    $this->db->where($where3);
                    $query4 = $this->db->get('dorm_roominfor');

                    //对象数组转数组
                    $peopleNumber = (array)$query4->result()[0];
                    //将宿舍实际人数+1
                    $peopleNumber_add = $peopleNumber['peopleNumber'] + 1;
                    //将宿舍剩余可住人数-1
                    $remainpeople_add = $peopleNumber['remainNumber'] - 1;
                    $data3 = array(
                        'peopleNumber' => $peopleNumber_add,
                        'remainNumber' => $remainpeople_add
                    );
                    //将宿舍实际人数,宿舍剩余可住人数进行更新
                    $this->db->where($where3);
                    $this->db->update('dorm_roominfor' ,$data3);
                    $query_roominfor = $this->db->affected_rows();

                    //将数据更新到水电表中
                    $this->db->insert('dorm_manday_calc' ,$data_manday);
                    $query_manday = $this->db->affected_rows();
                    // print_r($query_stay_retreat_tune);
                    // print_r($query_livepeople);
                    // print_r($query_bed);
                    // print_r($query_roominfor);
                    // print_r($query_manday);
                    // return;
                    if ($query_stay_retreat_tune  && $query_livepeople && $query_bed  && $query_roominfor && $query_manday) {
                        $return_su = array('res' => '40000', 'msg' => '入住成功', 'data' => $query1->row());
                        $result = json_encode($return_su);
                    } else {
                        $return_fa = array('res' => '40001', 'msg' => '入住失败', 'data' => '');
                        $result = json_encode($return_fa);
                    }
                }
            }
            return $result;
        }

        //  入住方法：申请人节点
        // public function check_in_applicant($RY_name, $RY_degsonid, $RY_sex, $RY_idcardnum, $RY_location, $RY_phone, $RY_address, $RY_emergency_contact, $RY_relationship, $RY_emergency_phone, $RY_reason)
        // {
        //     //必输字段未输则报错
        //     if ($RY_name == '' || $RY_degsonid == '' || $RY_sex == '' || $RY_idcardnum == '' || $RY_location == '' || $RY_phone == '' || $RY_address == '' || $RY_emergency_contact == '' || $RY_relationship == '' || $RY_emergency_phone == '' || $RY_reason == '') {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         $query = $this->db->get_where('dorm_stay_retreat_tune', array('SQ_RY_number' => $RY_degsonid));
        //         if ($query->num_rows() > 0) {
        //             $return = array('res' => '40002', 'msg' => '不允许重复入住');
        //             $result = json_encode($return);
        //         } else {
        //             //住宿/调宿/退宿表数据
        //             $type = 1; //已申请
        //             $data = array(
        //                 'SQ_RY_name' => $RY_name,
        //                 'SQ_RY_number' => $RY_degsonid,
        //                 'SQ_RY_sex' => $RY_sex,
        //                 'SQ_RY_idcardnum' => $RY_idcardnum,
        //                 'SQ_RY_location' => $RY_location,
        //                 'SQ_RY_phone' => $RY_phone,
        //                 'SQ_RY_address' => $RY_address,
        //                 'SQ_liaison' => $RY_emergency_contact,
        //                 'SQ_relationship' => $RY_relationship,
        //                 'SQ_liaison_phone' => $RY_emergency_phone,
        //                 'SQ_reason' => $RY_reason,
        //                 'SQ_type' => $type
        //             );
        //             //数据插入表
        //             $this->db->insert('dorm_stay_retreat_tune', $data);
        //             $return = $this->db->affected_rows();
        //             $query1 = $this->db->get_where('dorm_stay_retreat_tune', array('SQ_RY_number' => $RY_degsonid));
        //             // $query = $this->db->get_where('dorm_stay_retreat_tune' ,array('SQ_RY_idcardnum' => $RY_idcardnum ,'SQ_type' => $type));
        //             // $workflowname = '住宿';
        //             // $isremark = 0;
        //             // $date_now = date("Y-m-d");
        //             //流程表数据
        //             // $data1 = array(
        //             //     'requestid' => $query->row(0)->ID,
        //             //     'userid' => '',
        //             //     'workflowid' => $type,
        //             //     'workflowname' => $workflowname,
        //             //     'isremark' => $isremark,
        //             //     'careatedate' => $query->row(0)->SQ_date,
        //             //     'receivedate' => $date_now,
        //             //     'operatedate' => ''
        //             // );
        //             //数据插入表
        //             // $this->db->insert('dorm_process' ,$data1);
        //             if ($return) {
        //                 //申请成功
        //                 $return_su = array('res' => '40000', 'msg' => $RY_name . '申请成功', 'data' => $query1->row(0));
        //                 $result = json_encode($return_su);
        //             } else {
        //                 //申请失败
        //                 $return_fa = array('res' => '40001', 'msg' => $RY_name . '申请失败', 'data' => '');
        //                 $result = json_encode($return_fa);
        //             }
        //         }
        //     }
        //     return $result;
        // }

        // //入住方法：人资节点
        // public function check_in_hr($requestid ,$RY_department ,$RY_job ,$username)
        // {
        //     //必输字段未输则报错
        //     if($requestid == '')
        //     {
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     }
        //    else
        //     {
        //         //根据resquestid与id相等取值
        //         $query = $this->db->get_where('dorm_stay_retreat_tune' ,array('ID' ,$requestid));
        //         if($query->num_rows()>0)
        //         {
        //             //住宿/调宿/退宿表数据
        //             $data = array(
        //                 'SQ_RY_department' => $RY_department,
        //                 'SQ_RY_job' => $RY_job,
        //             );
        //             //更新表
        //             $this->db->where('ID' ,$requestid);
        //             $this->db->update('dorm_stay_retreat_tune' ,$data);
        //             $return1 = $this->db->affected_rows();
        //             if($return1)
        //             {
        //                 //当前日期
        //                 $date_now = date("Y-m-d");
        //                 //1:已操作 0：未操作
        //                 $isremark = 1;
        //                 $data1 = array(
        //                     'userid' => $username,
        //                     'isremark' => $isremark,
        //                     'operatedate' => $date_now,
        //                 );
        //                 //更新表
        //                 $this->db->where('requestid' ,$requestid);
        //                 $this->db->update('dorm_process' ,$data1);
        //                 //审批成功
        //                 $return_su = array('res' => '40000' ,'msg' => '审批成功' ,'data' => $requestid);
        //                 $result = json_encode($return_su);
        //             }
        //            else
        //             {   
        //                 //审批失败
        //                 $return_fa = array('res' => '40001' ,'msg' => '审批失败' ,'data' => $requestid);
        //                 $result = json_encode($return_fa);
        //             }
        //         }
        //        else
        //        {
        //            //没有符合条件的数据
        //            $return_fa1 = array('res' => '40001' ,'msg' => '没有查到符合条件的数据');
        //            $result = json_encode($return_fa1);
        //        }
        //     }
        //     return $result;
        // }

        // // 入住方法:宿管员节点
        // public function check_in_admin($RY_degsonid, $RY_sex, $RY_idcardnum, $RY_location, $RY_phone, $RY_address, $RY_emergency_contact, $RY_relationship, $RY_emergency_phone, $RY_reason ,$RY_name, $AR_number, $SSL_number, $SS_floor, $SS_number, $RY_bed ,$RY_remarks ,$Live_date)
        // {
        //     // if ($RY_name == '' || $AR_number == '' || $SSL_number == '' || $SS_floor == '' || $SS_number == '' || $RY_bed == ''|| $RY_degsonid == '' || $RY_sex == '' || $RY_idcardnum == '' || $RY_location == '' || $RY_phone == '' || $RY_address == '' || $RY_emergency_contact == '' || $RY_relationship == '' || $RY_emergency_phone == '' || $RY_reason == '' ) {  
        //     if ( $RY_name == '' || $Live_date == '' || $RY_sex == '' || $RY_idcardnum == '' || $RY_phone == '') {
        //         //必输字段未输则报错
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     } else {
        //         $RY_live_check = 1;
        //         $where1 = array('RY_idcardnum' => $RY_idcardnum ,'RY_live_check' => $RY_live_check);   
        //         $this->db->select('*');
        //         $this->db->where($where1);
        //         $query2 = $this->db->get('dorm_livepeople');
        //         if($query2->num_rows()>0)
        //         {
        //             $return = array('res' => '40002', 'msg' => '已入住，不允许重复入住');
        //             $result = json_encode($return);
        //         } else {
        //         $SQ_type = 1;
        //         $data = array(
        //             // 'SQ_RY_name' => $RY_name,
        //             // 'AR_number' => $AR_number,
        //             // 'SSL_number' => $SSL_number,
        //             // 'SS_floor' => $SS_floor,
        //             // 'SQ_SS_number' => $SS_number,
        //             // 'SQ_bed' => $RY_bed,
        //             // //    'SQ_wardrobe' => $RY_wardrobe,
        //             // 'SQ_RY_number' => $RY_degsonid,
        //             // 'SQ_RY_sex' => $RY_sex,
        //             // 'SQ_RY_idcardnum' => $RY_idcardnum,
        //             // 'SQ_RY_location' => $RY_location,
        //             // 'SQ_RY_phone' => $RY_phone,
        //             // 'SQ_RY_address' => $RY_address,
        //             // 'SQ_liaison' => $RY_emergency_contact,
        //             // 'SQ_relationship' => $RY_relationship,
        //             // 'SQ_liaison_phone' => $RY_emergency_phone,
        //             // 'SQ_reason' => $RY_reason,
        //             // 'SQ_type' => $type
        //             'SQ_live_date' => $Live_date,
        //             'AR_number' => $AR_number,
        //             'SSL_number' => $SSL_number,
        //             'SS_floor' => $SS_floor,
        //             'SQ_SS_number' => $SS_number,
        //             'SQ_RY_idcardnum' => $RY_idcardnum,
        //             'SQ_RY_number' => $RY_degsonid,
        //             'SQ_RY_name' => $RY_name,
        //             'SQ_RY_sex' => $RY_sex,
        //             'SQ_RY_phone' => $RY_phone,
        //             'SQ_RY_address' => $RY_address,
        //             'SQ_bed' => $RY_bed,
        //             'SQ_RY_location' => $RY_location,
        //             'SQ_liaison' => $RY_emergency_contact,
        //             'SQ_relationship' => $RY_relationship,
        //             'SQ_liaison_phone' => $RY_emergency_phone,
        //             'SQ_reason' => $RY_reason,
        //             'SQ_remarks' => $RY_remarks,
        //             'SQ_type' => $SQ_type
        //         );

        //         //人员入住信息数据
        //         $RY_live_check = 1;
        //         $data1 = array(
        //             'AR_number' => $AR_number,
        //             'SSL_number' => $SSL_number,
        //             'RY_number' => $SS_number,
        //             'RY_name' => $RY_name,
        //             'RY_idcardnum' => $RY_idcardnum,
        //             'RY_degsonid' => $RY_degsonid,
        //             'RY_sex' => $RY_sex,
        //             'SS_floor' => $SS_floor,
        //             'RY_degsonid' => $RY_degsonid,
        //             // 'RY_department' => $RY_department,
        //             // 'RY_job' => $RY_job,
        //             'RY_phone' => $RY_phone,
        //             'RY_live_check' => $RY_live_check,
        //             'RY_live_date' => $Live_date,
        //             'RY_bed' => $RY_bed,
        //             // 'RY_wardrobe' => $RY_wardrobe,
        //             'RY_remarks' => $RY_remarks,  
        //         );

        //         //信息写入床位
        //         $RY_whether = 1;
        //         $data2 = array(
        //             'AR_number' => $AR_number,
        //             'SSL_number' => $SSL_number,
        //             'SS_floor' => $SS_floor,
        //             'SS_number' => $SS_number,
        //             'RY_idcardnum' => $RY_idcardnum,
        //             'RY_name' => $RY_name,
        //             'RY_sex' => $RY_sex,
        //             'RY_degsonid' => $RY_degsonid,
        //             'RY_phone' => $RY_phone,
        //             'RY_whether' => $RY_whether,
        //             'RY_remarks' => $RY_remarks
        //         );

        //         $where2 = array('AR_number' => $AR_number ,'SSL_number' => $SSL_number ,'SS_number' => $SS_number ,'RY_bed' => $RY_bed);
        //         $this->db->insert('dorm_stay_retreat_tune', $data);
        //         $query = $this->db->affected_rows();
        //         $this->db->insert('dorm_livepeople' ,$data1);
        //         $query2 = $this->db->affected_rows();
        //         $this->db->where($where2);
        //         $this->db->update('dorm_bed' ,$data2);
        //         $query3 = $this->db->affected_rows();
        //         $where = array('SQ_RY_idcardnum' => $RY_idcardnum);
        //         $this->db->select('*');
        //         $this->db->where($where);
        //         $query1 = $this->db->get('dorm_stay_retreat_tune');
        //         $where3 = array('AR_number' => $AR_number ,'SSL_number' => $SSL_number ,'SS_number' => $SS_number);
        //         //取这个房间的实际入住人数进行更新
        //         $this->db->select('SS_peoplenumber');
        //         $this->db->where($where3);
        //         $query4 = $this->db->get('dorm_roominfor');
        //         $SS_peoplenumber = (array)$query4->result()[0];
        //         $SS_peoplenumber_add = $SS_peoplenumber['SS_peoplenumber'] + 1;
        //         $data3 = array(
        //             'SS_peoplenumber' => $SS_peoplenumber_add
        //         );
        //         $this->db->where($where3);
        //         $this->db->update('dorm_roominfor' ,$data3);
        //         $query5 = $this->db->affected_rows();
        //         if ($query && $query2 && $query3 && $query5) {
        //             $return_su = array('res' => '40000', 'msg' => '入住成功', 'data' => $query1->row());
        //             $result = json_encode($return_su);
        //         } else {
        //             $return_fa = array('res' => '40001', 'msg' => '入住失败', 'data' => '');
        //             $result = json_encode($return_fa);
        //         }
        //     }
            
        //     }
        //     return $result;
        // }

        // //人员入住方法
        // public function check_in_admin($RY_name ,$RY_live_date ,$RY_idcardnum ,$RY_sex ,$RY_degsonid ,$RY_department ,$RY_job ,$RY_phone ,$RY_remarks ,$SS_temporary ,$AR_number ,$SSL_number ,$SS_floor ,$RY_number ,$RY_bed ,$RY_wardrobe ,$SQ_use_number = array('SQ_use_number'))
        // {
        //     if($RY_name == '' || $RY_live_date == '' || $RY_idcardnum == '' || $RY_sex == '' || $RY_degsonid == '' || $RY_department == '' || $RY_job == '' || $RY_phone == '' || $RY_remarks == '' || $SS_temporary == '' || $AR_number == '' || $SSL_number == '' || $SS_floor == '' || $RY_number == '' || $RY_bed == '' || $RY_wardrobe == '' || $SQ_use_number == '')
        //     {
        //         //必输字段未输则报错
        //         $return = array('res' => '40002', 'msg' => '请输入必填字段');
        //         $result = json_encode($return);
        //     }
        //    else
        //     {

        //     //    $query = $this->db->get_where('dorm_process' ,array('requestid' => $requestid));
        //           $query1 = $this->db->get_where('dorm_sslmanage' ,array('AR_name' => $AR_number ,'SSL_name' => $SSL_number));
        //     //    $query2 = $this->db->get_where('dorm_stay_retreat_tune' ,array('ID' ,$requestid));
        //     //    $query3 = $this->db->get_where('dorm_livepeople' ,array('RY_idcardnum' => $query2->row(0)->SQ_RY_idcardnum));
        //     //    $query4 = $this->db->get_where('dorm_livepeople' ,array('RY_number' =>$query3->row(0)->RY_number));
        //     //    if($query4->num_rows()>0)
        //     //    {
        //     //        foreach($query4->result() as $row);
        //     //    }               
        //        if($query->num_rows()>0)
        //        {
        //            //住宿/调宿/退宿表数据
        //            $data = array(
        //                'AR_number' => $AR_number,
        //                'SSL_number' => $SSL_number,
        //                'SS_floor' => $SS_floor,
        //                'SQ_SS_number' => $RY_number,
        //                'SQ_bed' => $RY_bed,
        //                'SQ_wardrobe' => $RY_wardrobe
        //            );
        //            //人员入住信息数据
        //            $RY_live_check = 1;
        //            $data1 = array(
        //                'AR_number' => $AR_number,
        //                'SSL_number' => $SSL_number,
        //                'RY_number' => $RY_number,
        //                'RY_name' => $RY_name,
        //                'RY_idcardnum' => $RY_idcardnum,
        //                'RY_degsonid' => $RY_degsonid,
        //                'RY_sex' => $RY_sex,
        //                'RY_department' => $RY_department,
        //                'RY_job' => $RY_job,
        //             //    'RY_location' => $query2->row(0)->SQ_RY_location,
        //             //    'RY_address' => $query2->row(0)->SQ_RY_address,
        //                'RY_phone' => $RY_phone,
        //             //    'RY_emergency_contact' => $query2->row(0)->SQ_liaison,
        //             //    'RY_relationship' => $query2->row(0)->SQ_relationship,
        //             //    'RY_emergency_phone' => $query2->row(0)->SQ_liaison_phone,
        //             //    'RY_reason' => $query2->row(0)->SQ_reason,
        //                'RY_live_check' => $RY_live_check,
        //                'RY_live_date' => $RY_live_date,
        //                'RY_bed' => $RY_bed,
        //                'RY_wardrobe' => $RY_wardrobe,
        //                'RY_remarks' => $RY_remarks,  
        //            );
        //            //当前日期
        //            $date_now = date("Y-m-d");
        //            //领用物品更新
        //            $data2 = array(
        //                'RY_idcardnum' => $RY_idcardnum,
        //                'SQ_RY_number' => $RY_number,
        //                'SQ_RY_name' => $RY_name,
        //                'SQ_RY_sex' => $RY_sex,
        //                'SQ_RY_department' => $RY_department,
        //                'SQ_RY_job' => $RY_job,
        //                'SQ_SS_number' => $SS_number,
        //                'SQ_usedate' => $date_now,
        //                'SQ_roommate' => $row->SQ_RY_number ,$row->SQ_RY_name,
        //            );
        //            //更新表
        //            $this->db->where('ID' ,$requestid);
        //            $this->db->update('dorm_stay_retreat_tune' ,$data);
        //            $return = $this->db->affected_rows();
        //            //更新表
        //            $this->db->where('RY_idcardnum' ,$query2->row(0)->SQ_RY_idcardnum);
        //            $this->db->update('dorm_livepeople' ,$data1);
        //            //数据插入表
        //            $this->db->insert('dorm_article' ,$data2);
        //            if($return)
        //            {
        //                //入住成功
        //                $return_su = array('res' => '40000' ,'msg' => '入住成功' ,'');
        //                $result = json_encode($return_su);
        //            }
        //           else
        //            {
        //                //入住失败
        //                $return_fa = array('res' => '40001' ,'msg' => '入住失败');
        //                $result = json_encode($return_fa);
        //            }
        //        }
        //       else
        //        {
        //         //没有查到符合条件的数据
        //         $return_fa1 = array('res' => '40001' ,'msg' => '没有查到符合条件的数据');
        //         $result = json_encode($return_fa1);
        //       }
        //     }
        //     return $result;
        // }


        //入住筛选界面方法:取宿舍楼
        public function check_condition($area_number){
            //必输字段未输则报错
            if ($area_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                $query = $this->db->get_where('dorm_sslmanage', array('area_number' => $area_number));
                if ($query->num_rows() > 0) {
                    //查询成功
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'floor_number' => $query->row()->floor_number, 'floor_name' => $query->row()->floor_name);
                    $result = json_encode($return_su);
                } else {
                    //查询失败
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        //入住筛选界面方法:取楼层
        public function check_condition_floor($area_number, $floor_number){
            //必输字段未输则报错
            if ($area_number == '' || $floor_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                // $where = array('area_number' => $area_number, 'floor_number' => $floor_number);
                // $this->db->select('floorNumber');
                // $this->db->where($where);
                // $query = $this->db->get('dorm_sslmanage');
                $arr = [];
                if($area_number = '1585' && $floor_number = '1'){
                    $arr = [3,4,5,6];
                }elseif($area_number = '1585' && $floor_number = '2'){
                    $arr = [1];
                }elseif($area_number = '333' && $floor_number = '3'){
                    $arr = [1,2,3];
                }elseif($area_number = '178' && $floor_number = '4'){
                    $arr = [1];
                }
                if (count($arr) > 0) {
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $arr);
                    $result = json_encode($return_su);
                } else {
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
                return $result;
            }
        }

        //入住筛选界面方法:取宿舍
        public function check_condition_number($area_number ,$floor_number ,$dorm_floor ,$department ,$sex){
            $j = -1;
            $X = -1;
            $arr = [];
            if ($area_number == '' || $floor_number == '' || $dorm_floor == '' || $department == '' || $sex == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                $data_dorm_roominfor = $this->db->query("SELECT room_number FROM dorm_roominfor WHERE area_number LIKE '%".$area_number."%' AND floor_number LIKE '%".$floor_number."%' AND dorm_floor LIKE '%".$dorm_floor."%' AND room_sex LIKE '%".$sex."%' AND remainNumber != 0 ORDER BY remainNumber");
                // $query_dorm_roominfor = $data_dorm_roominfor->result(); 
                for($i=0 ;$i<$data_dorm_roominfor->num_rows() ;$i++){
                    $query_dorm_roominfor1 = $data_dorm_roominfor->result()[$i]->room_number;
                    $data_livepeople = $this->db->query("SELECT DISTINCT room_number FROM dorm_livepeople WHERE area_number LIKE '%".$area_number."%' AND floor_number LIKE '%".$floor_number."%' AND department LIKE '%".$department."%' AND room_number = '".$query_dorm_roominfor1."'");
                    if($data_livepeople->num_rows() > 0){
                        $j = $j+1;
                        $query_livepeople = $data_livepeople->result();
                        $query_livepeople = (array)$query_livepeople[0];
                        $arr[$j] = array(
                            'room_number' => $query_livepeople['room_number']
                        );
                    }
                }

                $arr_people = [];
                for($i=0 ;$i<count($arr) ;$i++){
                    $X = $X+1;
                    $room_number = $arr[$i]['room_number']; 
                    $data_people = $this->db->query("SELECT room_number ,remainNumber FROM dorm_roominfor WHERE room_number = '".$room_number."'");
                    $query_people = $data_people->result();
                    $query_people= (array)$query_people[0];
                    $arr_people[$X] = array(
                        'room_number' => $query_people['room_number'],
                        'remainNumber' => $query_people['remainNumber']
                    );
                }

                if (count($arr_people) > 0) {
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $arr_people);
                    $result = json_encode($return_su);
                } else {
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        //入住筛选界面方法:取床位与柜子
        public function check_condition_bed_table($area_number, $floor_number, $dorm_floor, $room_number){
            if ($area_number == '' | $floor_number == '' || $dorm_floor == '' | $room_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } else {
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'bedStatus' => 1, 'check_inWhether' => 0);
                //取床位
                $this->db->select('bedNumber');
                $this->db->where($where);
                $query = $this->db->get('dorm_bed');
                //取宿舍在用的柜子号
                // $this->db->select('RY_wardrobe');
                // $this->db->where($where);
                // $query1 = $this->db->get('dorm_wardrobe');
                // if($query->num_rows()>0 && $query1->num_rows()>0)
                if ($query->num_rows() > 0) {
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                } else {
                    $return_fa = array('res' => '40001', 'msg' => '查询失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }
}
